var express = require('express');
var router = express.Router();

var { insertAType, insertBType, selectAType, selectAlltype, deleteAType, deleteBType, updateType, selectTypes } = require("../db/type.js")

router.get('/', function (req, res, next) {
    res.send('respond with a resource');
});
// 新增一级分类
router.post('/addAType', function (req, res, next) {
    let body = req.body;
    let data = {}
    insertAType(body).then(e => {
        if (e) {
            data.message = "新增成功"
            data.code = "0"
        } else {
            data.message = "新增失败"
            data.code = "1"
            data.data = e
        }
        res.send(data)
    }).catch(e => {
        data.message = "系统出错"
        data.code = "err"
        data.data = e
        res.send(data)
    })
})
// 新增二级分类
router.post('/addBType', function (req, res, next) {
    let body = req.body;
    let data = {}
    insertBType(body).then(e => {
        if (e) {
            data.message = "新增成功"
            data.code = "0"
        } else {
            data.message = "新增失败"
            data.code = "1"
            data.data = e
        }
        res.send(data)
    }).catch(e => {
        data.message = "系统出错"
        data.code = "err"
        data.data = e
        res.send(data)
    })
})
// 获取一级分类
router.post('/getAType', function (req, res, next) {
    let data = {}
    selectAType().then(e => {
        if (e) {
            data.data = e;
            data.message = "获取成功"
            data.code = "0"
        } else {
            data.message = "获取失败"
            data.code = "1"
            data.data = e
        }
        res.send(data)
    }).catch(e => {
        data.message = "系统出错"
        data.code = "err"
        data.data = e
        res.send(data)
    })
})
// 所有分类的数据
router.post('/getTypes', function (req, res, next) {
    let data = {}
    selectAlltype().then(e => {
        if (e) {
            data.data = e;
            data.message = "获取成功"
            data.code = "0"
        } else {
            data.message = "获取失败"
            data.code = "1"
            data.data = e
        }
        res.send(data)
    }).catch(e => {
        data.message = "系统出错"
        data.code = "err"
        data.data = e
        res.send(data)
    })
})
// 删除指定一级分类
router.post('/delAType', function (req, res, next) {
    let data = {}
    let id = req.body.id
    deleteAType(id).then(e => {
        if (e) {
            data.message = "删除成功"
            data.code = "0"
            data.data = e
        } else {
            data.message = "删除失败"
            data.code = "1"
        }
        res.send(data)
    }).catch(e => {
        data.message = "系统出错"
        data.code = "err"
        data.data = e
        res.send(data)
    })
})
// 删除指定二级分类
router.post('/delBType', function (req, res, next) {
    let data = {}
    let id = req.body.id
    deleteBType(id).then(e => {
        if (e) {
            data.message = "删除成功"
            data.code = "0"
        } else {
            data.message = "删除失败"
            data.code = "1"
            data.data = e
        }
        res.send(data)
    }).catch(e => {
        data.message = "系统出错"
        data.code = "err"
        data.data = e
        res.send(data)
    })
})
// 修改分类
router.post('/updateType', function (req, res, next) {
    let data = {}
    let body = req.body
    updateType(body).then(e => {
        if (e) {
            data.message = "修改成功"
            data.code = "0"
        } else {
            data.message = "修改失败"
            data.code = "1"
            data.data = e
        }
        res.send(data)
    }).catch(e => {
        data.message = "系统出错"
        data.code = "err"
        data.data = e
        res.send(data)
    })
})
// 获取一二级分类
router.post('/getTypeOptions', function (req, res, next) {
    let data = {}
    selectTypes().then(e => {
        if (e) {
            data.message = "获取成功"
            data.code = "0"
            data.data = e
        } else {
            data.message = "获取失败"
            data.code = "1"
            data.data = e
        }
        res.send(data)
    }).catch(e => {
        data.message = "系统出错"
        data.code = "err"
        data.data = e
        res.send(data)
    })
})

module.exports = router;
